package com.nutiteq.mapper.forms;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

import com.nutiteq.MapComponent;
import com.nutiteq.MapItem;
import com.nutiteq.maps.CloudMade;
import com.nutiteq.maps.GeoMap;
import com.nutiteq.maps.OpenStreetMap;
import com.nutiteq.maps.StreamedCloudMadeMap;
import com.nutiteq.maps.UnstreamedDoubleSizedTilesMap;

public class ChangeMapList extends List implements CommandListener {
  public static final Command CHANGE_MAP_COMMAND = new Command("Change map", Command.ITEM, 0);

  private final Command back;
  private final Command change;

  private final MapComponent mapDisplay;

  private final MapItem mapItem;

  private final MIDlet midlet;

  private final Displayable returnTo;

  public ChangeMapList(final MIDlet midlet, final Displayable returnTo, final MapComponent mapDisplay, final MapItem mapItem) {
    super("Change Map", List.EXCLUSIVE);
    this.midlet = midlet;
    this.returnTo = returnTo;
    this.mapDisplay = mapDisplay;
    this.mapItem = mapItem;
    append("OSM direct big tile", null);
    append("CloudMade streaming (via Nutiteq)", null);
    append("CloudMade small tiles (OSM)", null);
    append("Double sized CloudMade tiles (OSM)", null);
    append("Last level added CloudMade tiles (OSM)", null);

    back = new Command("Back", Command.BACK, 0);
    change = new Command("Change", Command.ITEM, 0);

    addCommand(back);
    addCommand(change);

    final GeoMap usedMap = mapDisplay == null ? mapItem.getMap() : mapDisplay.getMap();

    if (usedMap == OpenStreetMap.MAPNIK) {
      setSelectedIndex(0, true);
    } else if (usedMap instanceof StreamedCloudMadeMap) {
      setSelectedIndex(1, true);
    } else if (usedMap instanceof CloudMade) {
      setSelectedIndex(2, true);
    } else if (usedMap instanceof UnstreamedDoubleSizedTilesMap) {
      setSelectedIndex(3, true);
    } else {
      setSelectedIndex(4, true);
    }

    setCommandListener(this);
  }

  public ChangeMapList(final MIDlet midlet, final Displayable returnTo, final MapItem mapItem) {
    this(midlet, returnTo, null, mapItem);
  }

  public ChangeMapList(final MIDlet midlet, final Displayable returnTo, final MapComponent mapDisplay) {
    this(midlet, returnTo, mapDisplay, null);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == change) {
      GeoMap nextMap = null;
      if (getSelectedIndex() == 0) {
        nextMap = OpenStreetMap.MAPNIK;
      } else if (getSelectedIndex() == 1) {
        nextMap = new StreamedCloudMadeMap();
      } else if (getSelectedIndex() == 2) {
        nextMap = new CloudMade("B37E70903EB540C1A07B3437AFA5DF2E", "test123", 64, 1);
      } else if (getSelectedIndex() == 3) {
        nextMap = new UnstreamedDoubleSizedTilesMap(new CloudMade("B37E70903EB540C1A07B3437AFA5DF2E", "test321", 256, 1));
      } else {
        final UnstreamedDoubleSizedTilesMap doubleSizedTilesMap = new UnstreamedDoubleSizedTilesMap(new CloudMade(
            "B37E70903EB540C1A07B3437AFA5DF2E", "demo333", 256, 1), true);
        nextMap = doubleSizedTilesMap;
      }
      if (mapDisplay == null) {
        mapItem.setMap(nextMap);
      } else {
        mapDisplay.setMap(nextMap);
      }
      Display.getDisplay(midlet).setCurrent(returnTo);
    } else if (cmd == back) {
      Display.getDisplay(midlet).setCurrent(returnTo);
    }
  }
}
